<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>MyBatis | Cying</title><meta name="keywords" content="JavaEE"><meta name="author" content="Cying-承影"><meta name="copyright" content="Cying-承影"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Mybatis    一、Mybatis 概述  支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。 MyBatis 可以对配置和原生 Map 使用简单的 XM 或注解，将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。   1.1.功能架构  AP">
<meta property="og:type" content="article">
<meta property="og:title" content="MyBatis">
<meta property="og:url" content="http://cyingk.github.io/2020/02/20/Mybatis/index.html">
<meta property="og:site_name" content="Cying">
<meta property="og:description" content="Mybatis    一、Mybatis 概述  支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。 MyBatis 可以对配置和原生 Map 使用简单的 XM 或注解，将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。   1.1.功能架构  AP">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=575314515,4268715674&fm=26&gp=0.jpg">
<meta property="article:published_time" content="2020-02-20T06:04:47.000Z">
<meta property="article:modified_time" content="2020-07-03T16:32:36.000Z">
<meta property="article:author" content="Cying-承影">
<meta property="article:tag" content="JavaEE">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=575314515,4268715674&fm=26&gp=0.jpg"><link rel="shortcut icon" href="/img/favicon.ico"><link rel="canonical" href="http://cyingk.github.io/2020/02/20/Mybatis/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//www.clarity.ms"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>(function(c,l,a,r,i,t,y){
    c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
    t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
    y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "4zndahq92q");</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: 'days',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"Traditional Chinese Activated Manually","cht_to_chs":"Simplified Chinese Activated Manually","day_to_night":"Dark Mode Activated Manually","night_to_day":"Light Mode Activated Manually","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isanchor: false
};

const saveToLocal = {
  // ttl 單位是 天
  set: function setWithExpiry(key, value, ttl) {
    if (ttl === 0) return
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}

// https://stackoverflow.com/questions/16839698/jquery-getscript-alternative-in-native-javascript
const getScript = url => new Promise((resolve, reject) => {
  const script = document.createElement('script')
  script.src = url
  script.async = true
  script.onerror = reject
  script.onload = script.onreadystatechange = function() {
    const loadState = this.readyState
    if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
    script.onload = script.onreadystatechange = null
    resolve()
  }
  document.head.appendChild(script)
})</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-07-04 00:32:36'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = '1'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}const fontSizeVal = saveToLocal.get('global-font-size')
if (fontSizeVal !== undefined) {
  document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
}})()</script><meta name="generator" content="Hexo 5.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">Loading...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">12</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">Categories</div><div class="length-num">3</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> Categories</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://5b0988e595225.cdn.sohucs.com/images/20190831/84729f5c5bb346208416445feb485df6.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Cying</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> Categories</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">MyBatis</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2020-02-20T06:04:47.000Z" title="Created 2020-02-20 14:04:47">2020-02-20</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2020-07-03T16:32:36.000Z" title="Updated 2020-07-04 00:32:36">2020-07-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Note/">Note</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">Word count:</span><span class="word-count">5.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">Reading time:</span><span>18min</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><html><head></head><body><h1 style="
           font-size: 60px;
           font-weight: bolder;
           border: none;
           "><center>Mybatis</center></h1>



<h1 id="一、Mybatis-概述"><a href="#一、Mybatis-概述" class="headerlink" title="一、Mybatis 概述"></a>一、Mybatis 概述</h1><blockquote>
<ul>
<li>支持定制化 <code>SQL</code>、存储过程以及高级映射的优秀的持久层框架。</li>
<li>避免了几乎所有的 <code>JDBC</code> 代码和手动设置参数以及获取结果集。</li>
<li><code>MyBatis</code> 可以对配置和原生 <code>Map</code> 使用简单的 <code>XM</code> 或注解，将接口和 <code>Java</code> 的 <code>POJOs(Plain Old Java Objects,普通的 Java对象)</code>映射成数据库中的记录。</li>
</ul>
</blockquote>
<h2 id="1-1-功能架构"><a href="#1-1-功能架构" class="headerlink" title="1.1.功能架构"></a>1.1.功能架构</h2><blockquote>
<ul>
<li>API接口层：提供给外部使用的接口 <code>API</code>，开发人员通过这些本地API来操纵数据库。接口层一接收到调用请求就会调用数据处理层来完成具体的数据处理。</li>
<li>数据处理层：负责具体的 <code>SQL</code> 查找、<code>SQL</code> 解析、<code>SQL</code> 执行和执行结果映射处理等。它主要的目的是根据调用的请求完成一次数据库操作。</li>
<li>基础支撑层：负责最基础的功能支撑，包括连接管理、事务管理、配置加载和缓存处理，这些都是共用的东西，将他们抽取出来作为最基础的组件。为上层的数据处理层提供最基础的支撑。</li>
</ul>
</blockquote>
<h2 id="1-2-优点"><a href="#1-2-优点" class="headerlink" title="1.2.优点"></a>1.2.优点</h2><blockquote>
<ul>
<li>简单易学：本身就很小且简单。没有任何第三方依赖，最简单安装只要两个 <code>jar</code> 文件+配置几个 <code>sql</code> 映射文件易于学习，易于使用，通过文档和源代码，可以比较完全的掌握它的设计思路和实现。</li>
<li>灵活：<code>mybatis</code> 不会对应用程序或者数据库的现有设计强加任何影响。 <code>sql</code> 写在 <code>xml</code> 里，便于统一管理和优化。通过 <code>sql</code> 基本上可以实现我们不使用数据访问框架可以实现的所有功能，或许更多。</li>
<li>解除 <code>sql</code> 与程序代码的耦合：通过提供 <code>DAL</code> 层，将业务逻辑和数据访问逻辑分离，使系统的设计更清晰，更易维护，更易单元测试。<code>sql</code> 和代码的分离，提高了可维护性。</li>
<li>提供映射标签，支持对象与数据库的 <code>orm</code> 字段关系映射</li>
<li>提供对象关系映射标签，支持对象关系组建维护</li>
<li>提供 <code>xml</code> 标签，支持编写动态 <code>sql</code>。</li>
</ul>
</blockquote>
<h2 id="1-3-缺点"><a href="#1-3-缺点" class="headerlink" title="1.3.缺点"></a>1.3.缺点</h2><blockquote>
<ul>
<li>编写 <code>SQL</code> 语句时工作量很大，尤其是字段多、关联表多时，更是如此。</li>
<li><code>SQL</code> 语句依赖于数据库，导致数据库移植性差，不能更换数据库。</li>
<li>框架还是比较简陋，功能尚有缺失，虽然简化了数据绑定代码，但是整个底层数据库查询实际还是要自己写的，工作量也比较大，而且不太容易适应快速数据库修改。</li>
<li>二级缓存机制不佳</li>
</ul>
</blockquote>
<h1 id="二、简单示例"><a href="#二、简单示例" class="headerlink" title="二、简单示例"></a>二、简单示例</h1><h2 id="2-1-pom-xml-导入依赖"><a href="#2-1-pom-xml-导入依赖" class="headerlink" title="2.1.pom.xml 导入依赖"></a>2.1.pom.xml 导入依赖</h2><p><strong>jdbc</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">groupId</span>></span>mysql<span class="tag">< /<span class="attr">groupId</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">artifactId</span>></span>mysql-connector-java<span class="tag">< /<span class="attr">artifactId</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">version</span>></span>8.0.17<span class="tag">< /<span class="attr">version</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">dependency</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>mybatis</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">groupId</span>></span>org.mybatis<span class="tag">< /<span class="attr">groupId</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">artifactId</span>></span>mybatis<span class="tag">< /<span class="attr">artifactId</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">version</span>></span>3.5.3<span class="tag">< /<span class="attr">version</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">dependency</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>junit</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">groupId</span>></span>junit<span class="tag">< /<span class="attr">groupId</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">artifactId</span>></span>junit<span class="tag">< /<span class="attr">artifactId</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">version</span>></span>4.12<span class="tag">< /<span class="attr">version</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">dependency</span>></span></span><br></pre></td></tr></tbody></table></figure></div>



<h2 id="2-2-编写-Mybatis-核心配置文件"><a href="#2-2-编写-Mybatis-核心配置文件" class="headerlink" title="2.2.编写 Mybatis 核心配置文件"></a>2.2.编写 Mybatis 核心配置文件</h2><p><strong>该文件名称不限，但是要放在 <code>src/main/resource</code> 文件夹</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!--?xml version="1.0" encoding="UTF-8" ?--></span></span><br><span class="line"><span class="meta">configuration</span> <span class="meta-keyword">PUBLIC</span></span><br><span class="line"><span class="meta">        <span class="meta-string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta">        <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-config.dtd"</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">configuration</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>></span></span><br><span class="line">        <span class="tag"><<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>></span></span><br><span class="line">            <span class="tag"><<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/></span></span><br><span class="line">            <span class="tag"><<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"POOLED"</span>></span></span><br><span class="line">                <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"驱动"</span>/></span></span><br><span class="line">                <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"jdbc:mysql://localhost:3306/数据库名"</span>/></span> # & 要用 <span class="symbol">&</span> </span><br><span class="line">                <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"用户名"</span>/></span></span><br><span class="line">                <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"密码"</span>/></span></span><br><span class="line">            <span class="tag">< /<span class="attr">dataSource</span>></span></span><br><span class="line">        <span class="tag">< /<span class="attr">environment</span>></span></span><br><span class="line">    <span class="tag">< /<span class="attr">environments</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line">        <span class="tag"><<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">"映射文件路径.xml"</span>/></span></span><br><span class="line">    <span class="tag">< /<span class="attr">mappers</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">configuration</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Cying 的数据库 url</span><br><span class="line">jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8<span class="symbol">&</span>useSSL=false<span class="symbol">&</span>serverTimezone=UTC<span class="symbol">&</span>rewriteBatchedStatements=true</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="2-3-编写一个实体类"><a href="#2-3-编写一个实体类" class="headerlink" title="2.3.编写一个实体类"></a>2.3.编写一个实体类</h2><p><strong>实体类中的属性都应私有，并生成 <code>Setter</code> 和 <code>Getter</code> 方法</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 实体类名 </span>{</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> String pass;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getId</span><span class="params">()</span> </span>{</span><br><span class="line">        <span class="keyword">return</span> id;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(<span class="keyword">int</span> id)</span> </span>{</span><br><span class="line">        <span class="keyword">this</span>.id = id;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>{</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>{</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getPass</span><span class="params">()</span> </span>{</span><br><span class="line">        <span class="keyword">return</span> pass;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setPass</span><span class="params">(String pass)</span> </span>{</span><br><span class="line">        <span class="keyword">this</span>.pass = pass;</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="2-4-编写获取会话的工具类"><a href="#2-4-编写获取会话的工具类" class="headerlink" title="2.4.编写获取会话的工具类"></a>2.4.编写获取会话的工具类</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.apache.ibatis.io.Resources;</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.session.SqlSession;</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.session.SqlSessionFactory;</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.session.SqlSessionFactoryBuilder;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.InputStream;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 工具类名 </span>{</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> SqlSessionFactory ssf;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> {</span><br><span class="line">        <span class="keyword">try</span> {</span><br><span class="line">            InputStream iStream = Resources.getResourceAsStream(<span class="string">"核心配置文件名"</span>);</span><br><span class="line">            ssf = <span class="keyword">new</span> SqlSessionFactoryBuilder().build(iStream);</span><br><span class="line">        } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        }</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> SqlSession 获取会话方法名() {</span><br><span class="line">        <span class="keyword">return</span> ssf.openSession();</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="2-5-编写会话接口"><a href="#2-5-编写会话接口" class="headerlink" title="2.5.编写会话接口"></a>2.5.编写会话接口</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> 会话接口名 </span>{</span><br><span class="line">    List<类名> 接口方法名();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="2-6-编写实体类映射文件"><a href="#2-6-编写实体类映射文件" class="headerlink" title="2.6.编写实体类映射文件"></a>2.6.编写实体类映射文件</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><!--?xml version=<span class="string"-->"1.0"</span> encoding=<span class="string">"UTF-8"</span> ?><br><span class="line"><br><span class="line">        <span class="string">"-//mybatis.org//DTD Mapper 3.0//EN"</span></span><br><span class="line">        <span class="string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span><br><span class="line"></span><br><span class="line"><mapper namespace="<span" class="string">"会话接口全路径"</mapper></span>></span><br><span class="line">    <select id="<span" class="string">"接口方法名" resultType="实体类全路径">        select * from mybatis.user;    < /select>< /mapper></select></span></pre></td></tr></tbody></table></figure></div>

<h2 id="2-7-编写测试类"><a href="#2-7-编写测试类" class="headerlink" title="2.7.编写测试类"></a>2.7.编写测试类</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.apache.ibatis.session.SqlSession;</span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserTest</span> </span>{</span><br><span class="line">    <span class="meta">@Test</span> <span class="comment">// @Test 注解来自 junit，标在方法上用于测试某个方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test</span><span class="params">()</span> </span>{</span><br><span class="line">        SqlSession session = 工具类名.获取会话方法名();</span><br><span class="line">        会话接口名 mapper = session.getMapper(会话接口名<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">        List<实体类> list = mapper.接口方法名();</span><br><span class="line">        <span class="keyword">for</span> (实体类 i : list) {</span><br><span class="line">            System.out.println(i);</span><br><span class="line">        }</span><br><span class="line">        session.close();</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="2-8-增删改查"><a href="#2-8-增删改查" class="headerlink" title="2.8.增删改查"></a>2.8.增删改查</h2><p><strong>对应标签</strong></p>
<blockquote>
<ul>
<li><code>select</code></li>
<li><code>insert:</code> 插入完成之后记得要提交事务 <code>会话.commit();</code></li>
<li><code>update:</code> 插入完成之后记得要提交事务 <code>会话.commit();</code></li>
<li><code>delete:</code> 插入完成之后记得要提交事务 <code>会话.commit();</code></li>
</ul>
</blockquote>
<p><strong>公用属性</strong></p>
<blockquote>
<ul>
<li><code>id:</code> 对应接口中的方法名</li>
<li><code>resultType:</code> 返回值的类型，如果是自定义实体类，记得写全路径，只有 <code><insert></insert></code> 有该属性</li>
<li><code>parameterType:</code> 参数类型<ul>
<li>如果是基本类型，在 <code>sql</code> 语句中用 <code>#{属性名}</code> 即可获取到方法里的属性</li>
<li>如果是一个对象，记得写该对象的全路径，在 <code>sql</code> 语句中用 <code>#{属性名}</code> 即可获取该对象的属性</li>
</ul>
</li>
</ul>
</blockquote>
<p><strong>Map插入</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Map<string, object> map = <span class="keyword">new</span> HashMap<string, object>();</string,></string,></span><br><span class="line">map.put(<span class="string">"属性1"</span>, <span class="number">3</span>);</span><br></pre></td></tr></tbody></table></figure></div>

<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"方法名"</span> <span class="attr">parameterType</span>=<span class="string">"map"</span> <span class="attr">resultType</span>=<span class="string">"对象全路径"</span>></span></span><br><span class="line">	select * from 数据库.数据表 where 字段1 = #{属性1};</span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h1 id="三、配置解析"><a href="#三、配置解析" class="headerlink" title="三、配置解析"></a>三、配置解析</h1><h2 id="3-1-核心配置文件"><a href="#3-1-核心配置文件" class="headerlink" title="3.1.核心配置文件"></a>3.1.核心配置文件</h2><blockquote>
<ul>
<li><code>properties</code></li>
<li><code>setting</code></li>
<li><code>typeAliases</code></li>
<li><code>typeHandlers</code></li>
<li><code>objectFactory</code></li>
<li><code>plugins</code></li>
<li><code>environments</code><ul>
<li><code>environment</code><ul>
<li><code>transactionManager</code></li>
<li><code>dataSource</code></li>
</ul>
</li>
</ul>
</li>
<li><code>databaseProvider</code></li>
<li><code>mappers</code></li>
</ul>
</blockquote>
<h2 id="3-2-环境配置"><a href="#3-2-环境配置" class="headerlink" title="3.2.环境配置"></a>3.2.环境配置</h2><blockquote>
<ul>
<li><code>MyBatis</code> 可以配置成适应多种环境，但每个 <code>SqlSession</code> 实例只能选择一种化境</li>
<li>默认的事务管理器是 <code>JDBC</code></li>
<li>默认的连接池是 <code>POOLED</code></li>
</ul>
</blockquote>
<h2 id="3-3-属性"><a href="#3-3-属性" class="headerlink" title="3.3.属性"></a>3.3.属性</h2><blockquote>
<ul>
<li>可以通过 <code>properties</code> 属性来实现引用配置文件</li>
<li>这些属性都是可以通过外部配置动态替换的，既可以在典型的 <code>Java</code> 文件中配置，也可以用 <code>properties</code> 元素的子元素来传递</li>
<li>当有了 <code>properties</code> 之后，就可以用 <code>${属性}</code> 来获取属性的值，比如将驱动、<code>url</code> 、用户名、密码独立成配置</li>
<li>当内嵌式和引入式都有同一个属性时，只有引入文件的属性有效</li>
</ul>
</blockquote>
<h3 id="3-3-1-内嵌式"><a href="#3-3-1-内嵌式" class="headerlink" title="3.3.1.内嵌式"></a>3.3.1.内嵌式</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">properties</span>></span></span><br><span class="line">	<span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"key1"</span> <span class="attr">value</span>=<span class="string">"value1"</span>/></span></span><br><span class="line">	<span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"key2"</span> <span class="attr">value</span>=<span class="string">"value2"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">properties</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="3-3-2-引入式"><a href="#3-3-2-引入式" class="headerlink" title="3.3.2.引入式"></a>3.3.2.引入式</h3><p><strong>在 <code>src/main/resource</code> 文件夹新建 <code>db.properties</code></strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">properties</div></div><figure class="highlight properties"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">key1</span>: <span class="string">value1</span></span><br><span class="line"><span class="attr">key2</span>: <span class="string">value2</span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>在核心配置文件中</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">properties</span> <span class="attr">resource</span>=<span class="string">"db.properties"</span>/></span></span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="3-4-别名"><a href="#3-4-别名" class="headerlink" title="3.4.别名"></a>3.4.别名</h2><h3 id="3-4-1-设置具体类"><a href="#3-4-1-设置具体类" class="headerlink" title="3.4.1.设置具体类"></a>3.4.1.设置具体类</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">typeAliases</span>></span></span><br><span class="line">	<span class="tag"><<span class="name">typeAlias</span> <span class="attr">type</span>=<span class="string">"类的全路径"</span> <span class="attr">alias</span>=<span class="string">"别名"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">typeAliases</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="3-4-2-设置包"><a href="#3-4-2-设置包" class="headerlink" title="3.4.2.设置包"></a>3.4.2.设置包</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">typeAliases</span>></span></span><br><span class="line">	<span class="tag"><<span class="name">typeAlias</span> <span class="attr">type</span>=<span class="string">"包的全路径"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">typeAliases</span>></span></span><br><span class="line"></span><br><span class="line">#实体类的别名就是实体类的首字母小写</span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="3-4-3-注解"><a href="#3-4-3-注解" class="headerlink" title="3.4.3.注解"></a>3.4.3.注解</h3><blockquote>
<ul>
<li>在实体类上面加 <code>@Alias("别名")</code> 的注解</li>
</ul>
</blockquote>
<h2 id="3-5-设置"><a href="#3-5-设置" class="headerlink" title="3.5.设置"></a>3.5.设置</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">settings</span>></span></span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"cacheEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line">    #该配置影响的所有映射器中配置的缓存的全局开关</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"lazyLoadingEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line">    #延迟加载的全局开关。当开启时，所有关联对象都会延迟加载。 特定关联关系中可通过设置fetchType属性来覆盖该项的开关状态</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"multipleResultSetsEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line">    #是否允许单一语句返回多结果集（需要兼容驱动）。	</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"useColumnLabel"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line">    #使用列标签代替列名。不同的驱动在这方面会有不同的表现， 具体可参考相关驱动文档或通过测试这两种不同的模式来观察所用驱动的结果。	</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"useGeneratedKeys"</span> <span class="attr">value</span>=<span class="string">"false"</span>/></span></span><br><span class="line">    #允许 JDBC 支持自动生成主键，需要驱动兼容。 如果设置为 true 则这个设置强制使用自动生成主键，尽管一些驱动不能兼容但仍可正常工作（比如 Derby）。	</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"autoMappingBehavior"</span> <span class="attr">value</span>=<span class="string">"PARTIAL"</span>/></span></span><br><span class="line">    #指定 MyBatis 应如何自动映射列到字段或属性。 NONE 表示取消自动映射；PARTIAL 只会自动映射没有定义嵌套结果集映射的结果集。 FULL 会自动映射任意复杂的结果集（无论是否嵌套）。	</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"defaultExecutorType"</span> <span class="attr">value</span>=<span class="string">"SIMPLE"</span>/></span></span><br><span class="line">    #配置默认的执行器。SIMPLE 就是普通的执行器；REUSE 执行器会重用预处理语句（prepared statements）； BATCH 执行器将重用语句并执行批量更新。	</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"defaultStatementTimeout"</span> <span class="attr">value</span>=<span class="string">"25"</span>/></span></span><br><span class="line">    #设置超时时间，它决定驱动等待数据库响应的秒数。	</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"defaultFetchSize"</span> <span class="attr">value</span>=<span class="string">"100"</span>/></span></span><br><span class="line">    #Sets the driver a hint as to control fetching size for return results. This parameter value can be override by a query setting.	</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"safeRowBoundsEnabled"</span> <span class="attr">value</span>=<span class="string">"false"</span>/></span></span><br><span class="line">    #允许在嵌套语句中使用分页（RowBounds）。	</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"mapUnderscoreToCamelCase"</span> <span class="attr">value</span>=<span class="string">"false"</span>/></span></span><br><span class="line">    #是否开启自动驼峰命名规则（camel case）映射，即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射。	</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"localCacheScope"</span> <span class="attr">value</span>=<span class="string">"SESSION"</span>/></span></span><br><span class="line">    #MyBatis 利用本地缓存机制（Local Cache）防止循环引用（circular references）和加速重复嵌套查询。 默认值为 SESSION，这种情况下会缓存一个会话中执行的所有查询。 若设置值为 STATEMENT，本地会话仅用在语句执行上，对相同 SqlSession 的不同调用将不会共享数据。	</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"jdbcTypeForNull"</span> <span class="attr">value</span>=<span class="string">"OTHER"</span>/></span></span><br><span class="line">    #当没有为参数提供特定的 JDBC 类型时，为空值指定 JDBC 类型。 某些驱动需要指定列的 JDBC 类型，多数情况直接用一般类型即可，比如 NULL、VARCHAR 或 OTHER。	</span><br><span class="line">  <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"lazyLoadTriggerMethods"</span> <span class="attr">value</span>=<span class="string">"equals,clone,hashCode,toString"</span>/></span></span><br><span class="line">    #指定哪个对象的方法触发一次延迟加载。	</span><br><span class="line"><span class="tag">< /<span class="attr">settings</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="3-6-映射器"><a href="#3-6-映射器" class="headerlink" title="3.6.映射器"></a>3.6.映射器</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line">	<span class="tag"><<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">"映射文件路径"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">mappers</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line">	<span class="tag"><<span class="name">package</span> <span class="attr">name</span>=<span class="string">"映射文件的包路径"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">mappers</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="3-7-生命周期"><a href="#3-7-生命周期" class="headerlink" title="3.7.生命周期"></a>3.7.生命周期</h2><p><a href="C:%5CUsers%5CCying%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20200220140219592.png" data-fancybox="group" data-caption="image-20200220140219592" class="fancybox"><img alt="image-20200220140219592" title="image-20200220140219592" data-src="C:%5CUsers%5CCying%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20200220140219592.png" class="lazyload"></a></p>
<h3 id="3-7-1-SqlSessionFactoryBuilder"><a href="#3-7-1-SqlSessionFactoryBuilder" class="headerlink" title="3.7.1.SqlSessionFactoryBuilder"></a>3.7.1.SqlSessionFactoryBuilder</h3><blockquote>
<ul>
<li>一旦创建了<code>SqlSessionFactory</code>， 就不再需要它了</li>
</ul>
</blockquote>
<h3 id="3-7-2-SqlSessionFactory"><a href="#3-7-2-SqlSessionFactory" class="headerlink" title="3.7.2.SqlSessionFactory"></a>3.7.2.SqlSessionFactory</h3><blockquote>
<ul>
<li>可以想象为:数据库连接池</li>
<li><code>SqlSessionFactory</code> 一旦被创建就应该在应用的运行期间一直存在， 没有任何理由丢弃它或重新创建<br>另-一个实例。</li>
<li>因此 <code>SqISessionFactory</code>的最佳作用域是应用作用域。</li>
<li>最简单的就是使用<strong>单例模式</strong>或者<strong>静态单例模式</strong>。</li>
</ul>
</blockquote>
<h3 id="3-7-3-SqlSession"><a href="#3-7-3-SqlSession" class="headerlink" title="3.7.3.SqlSession"></a>3.7.3.SqlSession</h3><blockquote>
<ul>
<li>连接到连接池的一一个请求</li>
<li><code>SqlSession</code> 的实例不是线程安全的，因此是不能被共享的，所以它的最佳的作用域是请求或方法作用域</li>
<li>用完之后需要赶紧关闭，否则资源被占用</li>
</ul>
</blockquote>
<h1 id="四、resultMap结果集映射"><a href="#四、resultMap结果集映射" class="headerlink" title="四、resultMap结果集映射"></a>四、resultMap结果集映射</h1><p>设有表</p>
<table>
<thead>
<tr>
<th>id</th>
<th>pwd</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>123</td>
</tr>
</tbody></table>
<p>设有实体类</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Info</span> </span>{</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line">    <span class="keyword">private</span> String password;</span><br><span class="line">    </span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<p>如果按照普通的方式，查询出来的 <code>password</code> 肯定为 <code>null</code>，因为表中没有 <code>password</code> 这个字段</p>
<p>*<em>解决方法1: *</em> 取别名</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"方法名"</span> <span class="attr">resultType</span>=<span class="string">"实体类类名"</span> <span class="attr">parameterType</span>=<span class="string">"?"</span>></span></span><br><span class="line">	select id, pwd as password from 数据库名.数据表名;</span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<p>*<em>解决方法2: *</em> resultMap</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"自定义Map名"</span> <span class="attr">type</span>=<span class="string">"实体类类名"</span>></span></span><br><span class="line">	<span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/></span></span><br><span class="line">    <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"pwd"</span> <span class="attr">property</span>=<span class="string">"password"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">resultMap</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"方法名"</span> <span class="attr">resultMap</span>=<span class="string">"自定义Map名"</span> <span class="attr">parameterType</span>=<span class="string">"?"</span>></span></span><br><span class="line">	select * from 数据库.数据表;</span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h1 id="五、分页"><a href="#五、分页" class="headerlink" title="五、分页"></a>五、分页</h1><h2 id="5-1-Limit-分页"><a href="#5-1-Limit-分页" class="headerlink" title="5.1.Limit 分页"></a>5.1.Limit 分页</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"方法名"</span> <span class="attr">parameterType</span>=<span class="string">"?"</span> <span class="attr">resultType</span>=<span class="string">"?"</span>></span></span><br><span class="line">	select * from 数据库.数据表 limit #{start}, #{size};</span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="5-2-RowBounds-分页"><a href="#5-2-RowBounds-分页" class="headerlink" title="5.2.RowBounds 分页"></a>5.2.RowBounds 分页</h2><h1 id="六、注解开发"><a href="#六、注解开发" class="headerlink" title="六、注解开发"></a>六、注解开发</h1><blockquote>
<ul>
<li>在接口的方法上面直接写 <code>sql</code> 表达式</li>
<li>当接口的方法有多个参数，则要在每个参数前加上 <code>@Param("名字")</code> 便于获取</li>
<li>但是在核心配置文件中要去映射到接口</li>
</ul>
</blockquote>
<h2 id="6-1-Select"><a href="#6-1-Select" class="headerlink" title="6.1.@Select()"></a>6.1.@Select()</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Select</span>(<span class="string">"select * from 表名"</span>)</span><br></pre></td></tr></tbody></table></figure></div>

<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Select</span>(<span class="string">"select * from 表名 where #{参数}"</span>)</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="6-2-Insert"><a href="#6-2-Insert" class="headerlink" title="6.2.@Insert()"></a>6.2.@Insert()</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Insert</span>(<span class="string">"insert into 表名 values(#{...}, #{...})"</span>)</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="6-3-Update"><a href="#6-3-Update" class="headerlink" title="6.3.@Update()"></a>6.3.@Update()</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Update</span>(<span class="string">"update 表名 set 字段名=#{...}"</span>)</span><br></pre></td></tr></tbody></table></figure></div>

<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Update</span>(<span class="string">"update 表名 set 字段名=#{...} where 条件"</span>)</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="6-4-Delete"><a href="#6-4-Delete" class="headerlink" title="6.4.@Delete()"></a>6.4.@Delete()</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Delete</span>(<span class="string">"delete from 表名 where 条件"</span>)</span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="6-5-Param"><a href="#6-5-Param" class="headerlink" title="6.5.@Param()"></a>6.5.@Param()</h2><blockquote>
<ul>
<li>基本类型的参数或者 <code>String</code> 类型需要加上</li>
<li>引用类型不需要</li>
<li>如果只有一个基本类型可以忽略</li>
</ul>
</blockquote>
<h1 id="七、Lombok"><a href="#七、Lombok" class="headerlink" title="七、Lombok"></a>七、Lombok</h1><h2 id="7-1-安装插件"><a href="#7-1-安装插件" class="headerlink" title="7.1.安装插件"></a>7.1.安装插件</h2><h2 id="7-2-导入依赖"><a href="#7-2-导入依赖" class="headerlink" title="7.2.导入依赖"></a>7.2.导入依赖</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">groupId</span>></span>org.projectlombok<span class="tag">< /<span class="attr">groupId</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">artifactId</span>></span>lombok<span class="tag">< /<span class="attr">artifactId</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">version</span>></span>1.18.10<span class="tag">< /<span class="attr">version</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">dependency</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="7-3-使用注解"><a href="#7-3-使用注解" class="headerlink" title="7.3.使用注解"></a>7.3.使用注解</h2><blockquote>
<ul>
<li><strong>@Getter/@Setter</strong> 作用类上，生成所有成员变量的 <code>getter/setter</code> 方法；作用于成员变量上，生成该成员变量的 <code>getter/setter</code> 方法。可以设定访问权限及是否懒加载等</li>
<li><strong>@ToString</strong> 作用于类，覆盖默认的 <code>toString()</code> 方法，可以通过 <code>of</code> 属性限定显示某些字段，通过 <code>exclude</code> 属性排除某些字段</li>
<li><strong>@EqualsAndHashCode</strong> 作用于类，覆盖默认的 <code>equals</code> 和 <code>hashCode</code></li>
<li><strong>@NonNull</strong> 主要作用于成员变量和参数中，标识不能为空，否则抛出空指针异常</li>
<li><strong>@NoArgsConstructor</strong>、<strong>@RequiredArgsConstructor</strong>、<strong>@AllArgsConstructor</strong> 作用于类上，用于生成构造函数。有 <code>staticName</code>、<code>access</code> 等属性</li>
<li><strong>@NoArgsConstructor</strong> 生成无参构造器</li>
<li><strong>@RequiredArgsConstructor</strong> 生成包含 <code>final</code> 和 <code>@NonNull</code> 注解的成员变量的构造器</li>
<li><strong>@AllArgsConstructor</strong> 生成全参构造器</li>
<li><strong>@Data</strong> 作用于类上，是以下注解的集合 <strong>@ToString</strong>、<strong>@EqualsAndHashCode</strong>、<strong>@Getter</strong>、<strong>@Setter</strong>、<strong>@RequiredArgsConstructor</strong></li>
<li><strong>@Builder</strong> 作用于类上，将类转变为建造者模式</li>
<li><strong>@Log</strong> 作用于类上，生成日志变量。针对不同的日志实现产品，有不同的注解</li>
<li><strong>@Cleanup</strong> 自动关闭资源，针对实现了 <code>java.io.Closeable</code> 接口的对象有效</li>
<li><strong>@SneakyThrows</strong> 可以对受检异常进行捕捉并抛出</li>
<li><strong>@Synchronized</strong> 作用于方法级别，可以替换 <code>synchronize</code> 关键字或 <code>lock</code> 锁，用处不大</li>
</ul>
</blockquote>
<h1 id="八、复杂查询"><a href="#八、复杂查询" class="headerlink" title="八、复杂查询"></a>八、复杂查询</h1><h2 id="8-1-多对一查询"><a href="#8-1-多对一查询" class="headerlink" title="8.1.多对一查询"></a>8.1.多对一查询</h2><h3 id="8-1-1-按查询嵌套处理"><a href="#8-1-1-按查询嵌套处理" class="headerlink" title="8.1.1.按查询嵌套处理"></a>8.1.1.按查询嵌套处理</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"方法名"</span> <span class="attr">resultMap</span>=<span class="string">"映射Map"</span>></span></span><br><span class="line">    select * from student</span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br><span class="line"><span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"映射Map"</span> <span class="attr">type</span>=<span class="string">"实体类"</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"属性名"</span> <span class="attr">column</span>=<span class="string">"字段名"</span>/></span></span><br><span class="line">    <span class="tag"><<span class="name">association</span> <span class="attr">property</span>=<span class="string">"属性名"</span>  <span class="attr">column</span>=<span class="string">"字段名"</span> <span class="attr">javaType</span>=<span class="string">"属性类型"</span> <span class="attr">select</span>=<span class="string">"调用的方法名"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">resultMap</span>></span></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"方法名"</span> <span class="attr">resultType</span>=<span class="string">"实体类"</span>></span></span><br><span class="line">    select * from teacher where id = #{id}</span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="8-1-2-按结果嵌套处理"><a href="#8-1-2-按结果嵌套处理" class="headerlink" title="8.1.2.按结果嵌套处理"></a>8.1.2.按结果嵌套处理</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"方法名"</span> <span class="attr">resultMap</span>=<span class="string">"映射Map"</span> ></span></span><br><span class="line">    select s.id sid, s.name sname , t.name tname</span><br><span class="line">    from student s,teacher t</span><br><span class="line">    where s.tid = t.id</span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"映射Map"</span> <span class="attr">type</span>=<span class="string">"实体类"</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">id</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"sid"</span>/></span></span><br><span class="line">    <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"name"</span> <span class="attr">column</span>=<span class="string">"sname"</span>/></span></span><br><span class="line">    <span class="tag"><<span class="name">association</span> <span class="attr">property</span>=<span class="string">"teacher"</span> <span class="attr">javaType</span>=<span class="string">"Teacher"</span>></span></span><br><span class="line">        <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"name"</span> <span class="attr">column</span>=<span class="string">"tname"</span>/></span></span><br><span class="line">    <span class="tag">< /<span class="attr">association</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">resultMap</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="8-2-一对多查询"><a href="#8-2-一对多查询" class="headerlink" title="8.2.一对多查询"></a>8.2.一对多查询</h2><h3 id="8-2-1-按查询嵌套处理"><a href="#8-2-1-按查询嵌套处理" class="headerlink" title="8.2.1.按查询嵌套处理"></a>8.2.1.按查询嵌套处理</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getTeacher2"</span> <span class="attr">resultMap</span>=<span class="string">"映射Map"</span>></span></span><br><span class="line">  select * from teacher where id = #{id}</span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br><span class="line"><span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"映射Map"</span> <span class="attr">type</span>=<span class="string">"Teacher"</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"students"</span> <span class="attr">javaType</span>=<span class="string">"ArrayList"</span> <span class="attr">ofType</span>=<span class="string">"Student"</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">select</span>=<span class="string">"子查询"</span>/></span></span><br><span class="line"><span class="tag">< /<span class="attr">resultMap</span>></span></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"子查询"</span> <span class="attr">resultType</span>=<span class="string">"Student"</span>></span></span><br><span class="line">    select * from student where tid = #{id}</span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h3 id="8-2-2-按结果嵌套处理"><a href="#8-2-2-按结果嵌套处理" class="headerlink" title="8.2.2.按结果嵌套处理"></a>8.2.2.按结果嵌套处理</h3><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getTeacher"</span> <span class="attr">resultMap</span>=<span class="string">"映射Map"</span>></span></span><br><span class="line">    select s.id sid, s.name sname , t.name tname, t.id tid</span><br><span class="line">    from student s,teacher t</span><br><span class="line">    where s.tid = t.id and t.id=#{id}</span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br><span class="line"><span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"映射Map"</span> <span class="attr">type</span>=<span class="string">"Teacher"</span>></span></span><br><span class="line">    # 如果查询出来的字段和属性名一致，则无需处理，这里着重处理不相同的情况以及一对多的情况</span><br><span class="line">    # collection 是获取集合</span><br><span class="line">    # javaType 用来标识实体类类型</span><br><span class="line">    # ofType 用来标识 List 集合的类型</span><br><span class="line">    <span class="tag"><<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"students"</span> <span class="attr">ofType</span>=<span class="string">"Student"</span>></span></span><br><span class="line">        <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"sid"</span>/></span></span><br><span class="line">        <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"name"</span> <span class="attr">column</span>=<span class="string">"sname"</span>/></span></span><br><span class="line">        <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"tid"</span> <span class="attr">column</span>=<span class="string">"tid"</span>/></span></span><br><span class="line">    <span class="tag">< /<span class="attr">collection</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">resultMap</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h1 id="九、动态-SQL"><a href="#九、动态-SQL" class="headerlink" title="九、动态 SQL"></a>九、动态 SQL</h1><h2 id="9-1-IF"><a href="#9-1-IF" class="headerlink" title="9.1.IF"></a>9.1.IF</h2><blockquote>
<ul>
<li>当方法里的某个属性不为空，即代表需要追加条件（如果不追加那就不要传这个属性）</li>
</ul>
</blockquote>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"方法名"</span> <span class="attr">parameterType</span>=<span class="string">"?"</span> <span class="attr">resultType</span>=<span class="string">"?"</span>></span></span><br><span class="line">	select * from ?</span><br><span class="line">    <span class="tag"><<span class="name">if</span> <span class="attr">test</span> = <span class="string">"某个属性 != null"</span>></span></span><br><span class="line">    	and 对应字段 = #{某个属性}</span><br><span class="line">    <span class="tag">< /<span class="attr">if</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="9-2-Where"><a href="#9-2-Where" class="headerlink" title="9.2.Where"></a>9.2.Where</h2><p><strong>Where-if</strong></p>
<blockquote>
<p>相当于正常的 <code>Java</code> 中的 <code>if</code> 语句,如果有多个条件组合判断的话用 <code>and</code>，<code>or</code> 连接</p>
</blockquote>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"方法名"</span> <span class="attr">parameterType</span>=<span class="string">"?"</span> <span class="attr">resultType</span>=<span class="string">"?"</span>></span></span><br><span class="line">    select * from ? </span><br><span class="line">    <span class="tag"><<span class="name">where</span>></span></span><br><span class="line">        <span class="tag"><<span class="name">if</span> <span class="attr">test</span>=<span class="string">"属性1 != null"</span>></span></span><br><span class="line">            对应字段 = #{属性1}</span><br><span class="line">        <span class="tag">< /<span class="attr">if</span>></span></span><br><span class="line">        <span class="tag"><<span class="name">if</span> <span class="attr">test</span>=<span class="string">"属性2 != null"</span>></span></span><br><span class="line">            and 对应字段 = #{属性2}</span><br><span class="line">        <span class="tag">< /<span class="attr">if</span>></span></span><br><span class="line">    <span class="tag">< /<span class="attr">where</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>Where-choose-when-otherwise</strong></p>
<blockquote>
<ul>
<li><code>choose</code> 就好像是<code>switch</code></li>
<li><code>when</code> 相当于 <code>case</code> ,可以有一种属性的对个判断,但不能同时去判断多个属性。只要有一个 <code>when</code> 满足,则 <code>break</code></li>
<li><code>otherwise</code> 就好像是 <code>default</code>，如果前面的 <code>when</code> 都不满足，则进入 <code>otherwise</code></li>
</ul>
</blockquote>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"方法名"</span> <span class="attr">resultType</span>=<span class="string">"?"</span> <span class="attr">parameterType</span>=<span class="string">"?"</span>></span></span><br><span class="line">       select * from ?</span><br><span class="line">       <span class="tag"><<span class="name">where</span>></span></span><br><span class="line">           <span class="tag"><<span class="name">choose</span>></span></span><br><span class="line">               <span class="tag"><<span class="name">when</span> <span class="attr">test</span>=<span class="string">"条件"</span>></span></span><br><span class="line">                   and ? = ?</span><br><span class="line">               <span class="tag">< /<span class="attr">when</span>></span></span><br><span class="line">               <span class="tag"><<span class="name">otherwise</span>></span></span><br><span class="line">                   and ? = ?</span><br><span class="line">               <span class="tag">< /<span class="attr">otherwise</span>></span></span><br><span class="line">           <span class="tag">< /<span class="attr">choose</span>></span></span><br><span class="line">       <span class="tag">< /<span class="attr">where</span>></span></span><br><span class="line">   <span class="tag">< /<span class="attr">select</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="9-3-Set"><a href="#9-3-Set" class="headerlink" title="9.3.Set"></a>9.3.Set</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">update</span> <span class="attr">id</span>=<span class="string">"?"</span> <span class="attr">parameterType</span>=<span class="string">"?"</span>></span></span><br><span class="line">    update ?</span><br><span class="line">      <span class="tag"><<span class="name">set</span>></span></span><br><span class="line">          <span class="tag"><<span class="name">if</span> <span class="attr">test</span>=<span class="string">"条件"</span>></span></span><br><span class="line">              ? = #{?},</span><br><span class="line">          <span class="tag">< /<span class="attr">if</span>></span></span><br><span class="line">          <span class="tag"><<span class="name">if</span> <span class="attr">test</span>=<span class="string">"条件"</span>></span></span><br><span class="line">              ? = #{?}</span><br><span class="line">          <span class="tag">< /<span class="attr">if</span>></span></span><br><span class="line">      <span class="tag">< /<span class="attr">set</span>></span></span><br><span class="line">    where ? = #{?};</span><br><span class="line"><span class="tag">< /<span class="attr">update</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="9-4-Sql-Include"><a href="#9-4-Sql-Include" class="headerlink" title="9.4.Sql-Include"></a>9.4.Sql-Include</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">sql</span> <span class="attr">id</span>=<span class="string">"自定义名称"</span>></span></span><br><span class="line">	<span class="tag"><<span class="name">if</span>></span></span><br><span class="line">        ...</span><br><span class="line">    <span class="tag">< /<span class="attr">if</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">sql</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"方法名"</span> <span class="attr">parameterType</span>=<span class="string">"?"</span> <span class="attr">resultType</span>=<span class="string">"?"</span>></span></span><br><span class="line">	select * from ?</span><br><span class="line">    <span class="tag"><<span class="name">where</span>></span></span><br><span class="line">    	<span class="tag"><<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"自定义名称"</span>></span><span class="tag">< /<span class="attr">include</span>></span></span><br><span class="line">    <span class="tag">< /<span class="attr">where</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<h2 id="9-5-Foreach"><a href="#9-5-Foreach" class="headerlink" title="9.5.Foreach"></a>9.5.Foreach</h2><div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">java</div></div><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testQueryBlogForeach</span><span class="params">()</span></span>{</span><br><span class="line">    SqlSession session = 工具类名.获取会话方法名();</span><br><span class="line">    ? mapper = session.getMapper(?<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">    HashMap map = <span class="keyword">new</span> HashMap();</span><br><span class="line">    </span><br><span class="line">    List<integer> ids = <span class="keyword">new</span> ArrayList<integer>();</integer></integer></span><br><span class="line">    ids.add(<span class="number">1</span>);</span><br><span class="line">    ids.add(<span class="number">2</span>);</span><br><span class="line">    ids.add(<span class="number">3</span>);</span><br><span class="line">    </span><br><span class="line">    map.put(<span class="string">"ids"</span>,ids);</span><br><span class="line">    </span><br><span class="line">    List<!--?--> list = mapper.方法名(map);</span><br><span class="line">    System.out.println(list);</span><br><span class="line">    session.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></div>

<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"方法名"</span> <span class="attr">parameterType</span>=<span class="string">"map"</span> <span class="attr">resultType</span>=<span class="string">"?"</span>></span></span><br><span class="line">    select * from ?</span><br><span class="line">    <span class="tag"><<span class="name">where</span>></span></span><br><span class="line">        <span class="tag"><<span class="name">foreach</span> <span class="attr">collection</span>=<span class="string">"ids"</span>  <span class="attr">item</span>=<span class="string">"id"</span> <span class="attr">open</span>=<span class="string">"and ("</span> <span class="attr">close</span>=<span class="string">")"</span> <span class="attr">separator</span>=<span class="string">"or"</span>></span></span><br><span class="line">            id=#{id}</span><br><span class="line">        <span class="tag">< /<span class="attr">foreach</span>></span></span><br><span class="line">    <span class="tag">< /<span class="attr">where</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">select</span>></span></span><br><span class="line"></span><br><span class="line">=> select * from ? where (id = ? or id = ? or id = ?)</span><br></pre></td></tr></tbody></table></figure></div>

<h1 id="十、缓存"><a href="#十、缓存" class="headerlink" title="十、缓存"></a>十、缓存</h1><blockquote>
<ul>
<li><code>MyBatis</code> 包含一个非常强大的查询缓存特性，它可以非常方便地定制和配置缓存。缓存可以极大的提升查询效率。</li>
<li><code>MyBatis</code> 系统中默认定义了两级缓存：<strong>一级缓存</strong> 和 <strong>二级缓存</strong><ul>
<li>默认情况下，只有一级缓存开启。（ <code>SqlSession</code> 级别的缓存，也称为本地缓存）</li>
<li>二级缓存需要手动开启和配置，他是基于 <code>namespace</code> 级别的缓存。</li>
<li>为了提高扩展性，<code>MyBatis</code> 定义了缓存接口 <code>Cache</code>。我们可以通过实现 <code>Cache</code> 接口来自定义二级缓存</li>
</ul>
</li>
</ul>
</blockquote>
<h2 id="10-1-一级缓存"><a href="#10-1-一级缓存" class="headerlink" title="10.1.一级缓存"></a>10.1.一级缓存</h2><blockquote>
<ul>
<li>一级缓存是 <code>SqlSession</code> 级别的缓存(默认是支持一级缓存，不需要再配置文件中配置一级缓存)，在操作数据库时</li>
<li>每个 <code>SqlSession</code> 类的实例对象中有一个数据结构（<code>HashMap</code>）可以用来存储缓存数据，不同的 <code>SqlSession</code> 类的实例对象缓存的数据区域（<code>HashMap</code>）是互不影响的。</li>
<li>当在同一个 <code>SqlSession</code> 中执行两次相同的 <code>sql</code> 语句时，第一次执行完毕会将数据写到内存中，第二次查询不执行 <code>sql</code> 直接从内存中获取。</li>
</ul>
</blockquote>
<h2 id="10-2-二级缓存"><a href="#10-2-二级缓存" class="headerlink" title="10.2.二级缓存"></a>10.2.二级缓存</h2><blockquote>
<ul>
<li>二级缓存是 <code>Mapper</code> 级别的缓存，多个 <code>SqlSession</code> 类的实例对象操作同一个 <code>Mapper</code> 配置文件中的 <code>sql</code> 语句，多个 <code>SqlSession</code> 类的实例对象可以共用二级缓存，二级缓存是跨 <code>SqlSession</code> 的。</li>
<li>一个 <code>Mapper</code> 有一个自己的二级缓存区域（按照 <code>namespace</code> 划分），两个 <code>Mapper</code> 的 <code>namespace</code> 如果相同，那么这两个<code>Mapper</code> 执行的 <code>sql</code> 查询会被缓存在同一个二级缓存中。</li>
<li>要开启二级缓存需要在配置文件中设置 <code>cacheEnabled</code>属性为 <code>true</code>。</li>
</ul>
</blockquote>
<p><strong>开启二级缓存</strong></p>
<p><strong>在核心配置文件配置</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"cacheEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line"></span><br><span class="line"># 这个默认是开启，只不过一般会显式的给写出来</span><br></pre></td></tr></tbody></table></figure></div>

<p>在 <code>Mapper</code> 中配置</p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">cache</span>/></span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>效果</strong></p>
<blockquote>
<ul>
<li>映射语句文件中的所有 <code>select</code> 语句的结果将会被缓存。</li>
<li>映射语句文件中的所有 <code>insert</code>、<code>update</code> 和 <code>delete</code> 语句会刷新缓存。</li>
<li>缓存会使用最近最少使用算法（<code>LRU</code>, <code>Least Recently Used</code>）算法来清除不需要的缓存。</li>
<li>缓存不会定时进行刷新（也就是说，没有刷新间隔）。</li>
<li>缓存会保存列表或对象（无论查询方法返回哪种）的 1024 个引用。</li>
<li>缓存会被视为读/写缓存，这意味着获取到的对象并不是共享的，可以安全地被调用者修改，而不干扰其他调用者或线程所做的潜在修改。</li>
</ul>
</blockquote>
<p><strong>添加配置</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">cache</span></span></span><br><span class="line"><span class="tag">  <span class="attr">eviction</span>=<span class="string">"FIFO"</span></span></span><br><span class="line"><span class="tag">  <span class="attr">flushInterval</span>=<span class="string">"60000"</span></span></span><br><span class="line"><span class="tag">  <span class="attr">size</span>=<span class="string">"512"</span></span></span><br><span class="line"><span class="tag">  <span class="attr">readOnly</span>=<span class="string">"true"</span>/></span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>讲解</strong></p>
<blockquote>
<ul>
<li><code>eviction</code> 清除策略<ul>
<li><code>LRU</code> – 最近最少使用：移除最长时间不被使用的对象。</li>
<li><code>FIFO</code> – 先进先出：按对象进入缓存的顺序来移除它们。</li>
<li><code>SOFT</code> – 软引用：基于垃圾回收器状态和软引用规则移除对象。</li>
<li><code>WEAK</code> – 弱引用：更积极地基于垃圾收集器状态和弱引用规则移除对象。</li>
</ul>
</li>
<li><code>flushInterval</code>（刷新间隔）属性可以被设置为任意的正整数，设置的值应该是一个以毫秒为单位的合理时间量。 默认情况是不设置，也就是没有刷新间隔，缓存仅仅会在调用语句时刷新。</li>
<li><code>size</code>（引用数目）属性可以被设置为任意正整数，要注意欲缓存对象的大小和运行环境中可用的内存资源。默认值是 1024。</li>
<li><code>readOnly</code>（只读）属性可以被设置为 true 或 false。只读的缓存会给所有调用者返回缓存对象的相同实例。 因此这些对象不能被修改。这就提供了可观的性能提升。而可读写的缓存会（通过序列化）返回缓存对象的拷贝。 速度上会慢一些，但是更安全，因此默认值是 false。</li>
</ul>
</blockquote>
<h2 id="10-3-自定义缓存"><a href="#10-3-自定义缓存" class="headerlink" title="10.3.自定义缓存"></a>10.3.自定义缓存</h2><p><strong>在 <code>pom.xml</code> 导入依赖</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">groupId</span>></span>org.mybatis.caches<span class="tag">< /<span class="attr">groupId</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">artifactId</span>></span>mybatis-ehcache<span class="tag">< /<span class="attr">artifactId</span>></span></span><br><span class="line">    <span class="tag"><<span class="name">version</span>></span>1.1.0<span class="tag">< /<span class="attr">version</span>></span></span><br><span class="line"><span class="tag">< /<span class="attr">dependency</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>在 <code>mapper.xml</code> 配置</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">cache</span> <span class="attr">type</span> = <span class="string">“org.mybatis.caches.ehcache.EhcacheCache”</span> /></span></span><br></pre></td></tr></tbody></table></figure></div>

<p><strong>在 <code>src/main/resources</code> 文件夹新建 ehcache</strong></p>
<div class="code-area-wrap"><div class="highlight-tools"><i class="fa fa-angle-down code-expand" aria-hidden="true"></i><div class="code_lang">xml</div></div><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!--?xml version="1.0" encoding="UTF-8"?--></span></span><br><span class="line"><span class="tag"><<span class="name">ehcache</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">xsi:noNamespaceSchemaLocation</span>=<span class="string">"http://ehcache.org/ehcache.xsd"</span></span></span><br><span class="line"><span class="tag">         <span class="attr">updateCheck</span>=<span class="string">"false"</span>></span></span><br><span class="line">    <span class="comment"><!--</span></span><br><span class="line"><span class="comment">       diskStore：为缓存路径，ehcache分为内存和磁盘两级，此属性定义磁盘的缓存位置。参数解释如下：</span></span><br><span class="line"><span class="comment">       user.home – 用户主目录</span></span><br><span class="line"><span class="comment">       user.dir  – 用户当前工作目录</span></span><br><span class="line"><span class="comment">       java.io.tmpdir – 默认临时文件路径</span></span><br><span class="line"><span class="comment">     --></span></span><br><span class="line">    <span class="tag"><<span class="name">diskStore</span> <span class="attr">path</span>=<span class="string">"./tmpdir/Tmp_EhCache"</span>/></span></span><br><span class="line">    </span><br><span class="line">    <span class="tag"><<span class="name">defaultCache</span></span></span><br><span class="line"><span class="tag">            <span class="attr">eternal</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">maxElementsInMemory</span>=<span class="string">"10000"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">overflowToDisk</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">diskPersistent</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">timeToIdleSeconds</span>=<span class="string">"1800"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">timeToLiveSeconds</span>=<span class="string">"259200"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">memoryStoreEvictionPolicy</span>=<span class="string">"LRU"</span>/></span></span><br><span class="line"> </span><br><span class="line">    <span class="tag"><<span class="name">cache</span></span></span><br><span class="line"><span class="tag">            <span class="attr">name</span>=<span class="string">"cloud_user"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">eternal</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">maxElementsInMemory</span>=<span class="string">"5000"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">overflowToDisk</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">diskPersistent</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">timeToIdleSeconds</span>=<span class="string">"1800"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">timeToLiveSeconds</span>=<span class="string">"1800"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">memoryStoreEvictionPolicy</span>=<span class="string">"LRU"</span>/></span></span><br><span class="line">    /*</span><br><span class="line">      defaultCache：默认缓存策略，当ehcache找不到定义的缓存时，则使用这个缓存策略。</span><br><span class="line">                    只能定义一个。</span><br><span class="line">      name:缓存名称。</span><br><span class="line">      maxElementsInMemory:缓存最大数目</span><br><span class="line">      maxElementsOnDisk：硬盘最大缓存个数。</span><br><span class="line">      eternal:对象是否永久有效，一但设置了，timeout将不起作用。</span><br><span class="line">      overflowToDisk:是否保存到磁盘，当系统当机时</span><br><span class="line">      timeToIdleSeconds:设置对象在失效前的允许闲置时间（单位：秒）。</span><br><span class="line">                        仅当eternal=false对象不是永久有效时使用</span><br><span class="line">                        可选属性，默认值是0，也就是可闲置时间无穷大。</span><br><span class="line">      timeToLiveSeconds:设置对象在失效前允许存活时间（单位：秒）。</span><br><span class="line">                        最大时间介于创建时间和失效时间之间。</span><br><span class="line">                        仅当eternal=false对象不是永久有效时使用</span><br><span class="line">                        默认是0.，也就是对象存活时间无穷大。</span><br><span class="line">      diskPersistent：是否缓存虚拟机重启期数据</span><br><span class="line">                      Whether the disk store persists between restarts of the Virtual Machine.</span><br><span class="line">                      The default value is false.</span><br><span class="line">      diskSpoolBufferSizeMB：这个参数设置DiskStore（磁盘缓存）的缓存区大小。</span><br><span class="line">                             默认是30MB。每个Cache都应该有自己的一个缓冲区。</span><br><span class="line">      diskExpiryThreadIntervalSeconds：磁盘失效线程运行时间间隔，默认是120秒。</span><br><span class="line">      memoryStoreEvictionPolicy：当达到maxElementsInMemory限制时，Ehcache将会根据指定的策略去清理内存。</span><br><span class="line">                                 默认策略是LRU（最近最少使用）。</span><br><span class="line">                                 你可以设置为FIFO（先进先出）</span><br><span class="line">                                 或是LFU（较少使用）。</span><br><span class="line">      clearOnFlush：内存数量最大时是否清除。</span><br><span class="line">      memoryStoreEvictionPolicy:可选策略有：</span><br><span class="line">                                    LRU（最近最少使用，默认策略）、</span><br><span class="line">                                    FIFO（先进先出）、</span><br><span class="line">                                    LFU（最少访问次数）。</span><br><span class="line">      FIFO，first in first out，这个是大家最熟的，先进先出。</span><br><span class="line">      LFU， Less Frequently Used，就是上面例子中使用的策略，直白一点就是讲一直以来最少被使用的。</span><br><span class="line">                                  如上面所讲，缓存的元素有一个hit属性，hit值最小的将会被清出缓存。</span><br><span class="line">      LRU，Least Recently Used，最近最少使用的，缓存的元素有一个时间戳，当缓存容量满了，</span><br><span class="line">                                而又需要腾出地方来缓存新的元素的时候，</span><br><span class="line">                                那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。</span><br><span class="line">    */</span><br><span class="line"></span><br><span class="line"><span class="tag">< /<span class="attr">ehcache</span>></span></span><br></pre></td></tr></tbody></table></figure></div>

</body></html></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="mailto:undefined">Cying-承影</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://cyingk.github.io/2020/02/20/Mybatis/">http://cyingk.github.io/2020/02/20/Mybatis/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JavaEE/">JavaEE</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/02/26/UML/"><img class="prev-cover" src="https://upload-images.jianshu.io/upload_images/5336514-985132f6977c5d33.png?imageMogr2/auto-orient/strip|imageView2/2/w/694/format/webp" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">UML</div></div></a></div><div class="next-post pull-right"><a href="/2020/02/16/SpringMVC/"><img class="next-cover" src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=790255512,3591398303&amp;fm=26&amp;gp=0.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">SpringMVC</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> Related Articles</span></div><div class="relatedPosts-list"><div><a href="/2020/08/06/Linux-And-Docker/" title="SpringCloud With Docker"><img class="cover" src="https://img-blog.csdnimg.cn/202008061653587.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-08-06</div><div class="title">SpringCloud With Docker</div></div></a></div><div><a href="/2020/05/05/SpringCloud/" title="SpringCloud"><img class="cover" src="http://img3.imgtn.bdimg.com/it/u=1799420697,1254911040&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-05-05</div><div class="title">SpringCloud</div></div></a></div><div><a href="/2020/04/05/SpringBoot/" title="SpringBoot"><img class="cover" src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4157804759,1050439081&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-05</div><div class="title">SpringBoot</div></div></a></div><div><a href="/2020/02/16/SpringMVC/" title="SpringMVC"><img class="cover" src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=790255512,3591398303&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-02-16</div><div class="title">SpringMVC</div></div></a></div><div><a href="/2020/02/15/Hibernate/" title="Hibernate"><img class="cover" src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=607365457,3589909035&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-02-15</div><div class="title">Hibernate</div></div></a></div><div><a href="/2020/02/01/Struts2/" title="Struts2"><img class="cover" src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2597327423,2034793762&fm=26&gp=0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-02-01</div><div class="title">Struts2</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">1.</span> <span class="toc-text">Mybatis</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81Mybatis-%E6%A6%82%E8%BF%B0"><span class="toc-number">2.</span> <span class="toc-text">一、Mybatis 概述</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-%E5%8A%9F%E8%83%BD%E6%9E%B6%E6%9E%84"><span class="toc-number">2.1.</span> <span class="toc-text">1.1.功能架构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-%E4%BC%98%E7%82%B9"><span class="toc-number">2.2.</span> <span class="toc-text">1.2.优点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-%E7%BC%BA%E7%82%B9"><span class="toc-number">2.3.</span> <span class="toc-text">1.3.缺点</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E7%AE%80%E5%8D%95%E7%A4%BA%E4%BE%8B"><span class="toc-number">3.</span> <span class="toc-text">二、简单示例</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-pom-xml-%E5%AF%BC%E5%85%A5%E4%BE%9D%E8%B5%96"><span class="toc-number">3.1.</span> <span class="toc-text">2.1.pom.xml 导入依赖</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E7%BC%96%E5%86%99-Mybatis-%E6%A0%B8%E5%BF%83%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">3.2.</span> <span class="toc-text">2.2.编写 Mybatis 核心配置文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E5%AE%9E%E4%BD%93%E7%B1%BB"><span class="toc-number">3.3.</span> <span class="toc-text">2.3.编写一个实体类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E7%BC%96%E5%86%99%E8%8E%B7%E5%8F%96%E4%BC%9A%E8%AF%9D%E7%9A%84%E5%B7%A5%E5%85%B7%E7%B1%BB"><span class="toc-number">3.4.</span> <span class="toc-text">2.4.编写获取会话的工具类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-5-%E7%BC%96%E5%86%99%E4%BC%9A%E8%AF%9D%E6%8E%A5%E5%8F%A3"><span class="toc-number">3.5.</span> <span class="toc-text">2.5.编写会话接口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-6-%E7%BC%96%E5%86%99%E5%AE%9E%E4%BD%93%E7%B1%BB%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6"><span class="toc-number">3.6.</span> <span class="toc-text">2.6.编写实体类映射文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-7-%E7%BC%96%E5%86%99%E6%B5%8B%E8%AF%95%E7%B1%BB"><span class="toc-number">3.7.</span> <span class="toc-text">2.7.编写测试类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-8-%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5"><span class="toc-number">3.8.</span> <span class="toc-text">2.8.增删改查</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E9%85%8D%E7%BD%AE%E8%A7%A3%E6%9E%90"><span class="toc-number">4.</span> <span class="toc-text">三、配置解析</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-%E6%A0%B8%E5%BF%83%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">4.1.</span> <span class="toc-text">3.1.核心配置文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE"><span class="toc-number">4.2.</span> <span class="toc-text">3.2.环境配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E5%B1%9E%E6%80%A7"><span class="toc-number">4.3.</span> <span class="toc-text">3.3.属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-1-%E5%86%85%E5%B5%8C%E5%BC%8F"><span class="toc-number">4.3.1.</span> <span class="toc-text">3.3.1.内嵌式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-2-%E5%BC%95%E5%85%A5%E5%BC%8F"><span class="toc-number">4.3.2.</span> <span class="toc-text">3.3.2.引入式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-4-%E5%88%AB%E5%90%8D"><span class="toc-number">4.4.</span> <span class="toc-text">3.4.别名</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-1-%E8%AE%BE%E7%BD%AE%E5%85%B7%E4%BD%93%E7%B1%BB"><span class="toc-number">4.4.1.</span> <span class="toc-text">3.4.1.设置具体类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-2-%E8%AE%BE%E7%BD%AE%E5%8C%85"><span class="toc-number">4.4.2.</span> <span class="toc-text">3.4.2.设置包</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-3-%E6%B3%A8%E8%A7%A3"><span class="toc-number">4.4.3.</span> <span class="toc-text">3.4.3.注解</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-5-%E8%AE%BE%E7%BD%AE"><span class="toc-number">4.5.</span> <span class="toc-text">3.5.设置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-6-%E6%98%A0%E5%B0%84%E5%99%A8"><span class="toc-number">4.6.</span> <span class="toc-text">3.6.映射器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-7-%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-number">4.7.</span> <span class="toc-text">3.7.生命周期</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-7-1-SqlSessionFactoryBuilder"><span class="toc-number">4.7.1.</span> <span class="toc-text">3.7.1.SqlSessionFactoryBuilder</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-7-2-SqlSessionFactory"><span class="toc-number">4.7.2.</span> <span class="toc-text">3.7.2.SqlSessionFactory</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-7-3-SqlSession"><span class="toc-number">4.7.3.</span> <span class="toc-text">3.7.3.SqlSession</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9B%9B%E3%80%81resultMap%E7%BB%93%E6%9E%9C%E9%9B%86%E6%98%A0%E5%B0%84"><span class="toc-number">5.</span> <span class="toc-text">四、resultMap结果集映射</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E5%88%86%E9%A1%B5"><span class="toc-number">6.</span> <span class="toc-text">五、分页</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1-Limit-%E5%88%86%E9%A1%B5"><span class="toc-number">6.1.</span> <span class="toc-text">5.1.Limit 分页</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2-RowBounds-%E5%88%86%E9%A1%B5"><span class="toc-number">6.2.</span> <span class="toc-text">5.2.RowBounds 分页</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E6%B3%A8%E8%A7%A3%E5%BC%80%E5%8F%91"><span class="toc-number">7.</span> <span class="toc-text">六、注解开发</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#6-1-Select"><span class="toc-number">7.1.</span> <span class="toc-text">6.1.@Select()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-2-Insert"><span class="toc-number">7.2.</span> <span class="toc-text">6.2.@Insert()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-3-Update"><span class="toc-number">7.3.</span> <span class="toc-text">6.3.@Update()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-4-Delete"><span class="toc-number">7.4.</span> <span class="toc-text">6.4.@Delete()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-5-Param"><span class="toc-number">7.5.</span> <span class="toc-text">6.5.@Param()</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%83%E3%80%81Lombok"><span class="toc-number">8.</span> <span class="toc-text">七、Lombok</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#7-1-%E5%AE%89%E8%A3%85%E6%8F%92%E4%BB%B6"><span class="toc-number">8.1.</span> <span class="toc-text">7.1.安装插件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-2-%E5%AF%BC%E5%85%A5%E4%BE%9D%E8%B5%96"><span class="toc-number">8.2.</span> <span class="toc-text">7.2.导入依赖</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-3-%E4%BD%BF%E7%94%A8%E6%B3%A8%E8%A7%A3"><span class="toc-number">8.3.</span> <span class="toc-text">7.3.使用注解</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AB%E3%80%81%E5%A4%8D%E6%9D%82%E6%9F%A5%E8%AF%A2"><span class="toc-number">9.</span> <span class="toc-text">八、复杂查询</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#8-1-%E5%A4%9A%E5%AF%B9%E4%B8%80%E6%9F%A5%E8%AF%A2"><span class="toc-number">9.1.</span> <span class="toc-text">8.1.多对一查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#8-1-1-%E6%8C%89%E6%9F%A5%E8%AF%A2%E5%B5%8C%E5%A5%97%E5%A4%84%E7%90%86"><span class="toc-number">9.1.1.</span> <span class="toc-text">8.1.1.按查询嵌套处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-1-2-%E6%8C%89%E7%BB%93%E6%9E%9C%E5%B5%8C%E5%A5%97%E5%A4%84%E7%90%86"><span class="toc-number">9.1.2.</span> <span class="toc-text">8.1.2.按结果嵌套处理</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-2-%E4%B8%80%E5%AF%B9%E5%A4%9A%E6%9F%A5%E8%AF%A2"><span class="toc-number">9.2.</span> <span class="toc-text">8.2.一对多查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#8-2-1-%E6%8C%89%E6%9F%A5%E8%AF%A2%E5%B5%8C%E5%A5%97%E5%A4%84%E7%90%86"><span class="toc-number">9.2.1.</span> <span class="toc-text">8.2.1.按查询嵌套处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-2-2-%E6%8C%89%E7%BB%93%E6%9E%9C%E5%B5%8C%E5%A5%97%E5%A4%84%E7%90%86"><span class="toc-number">9.2.2.</span> <span class="toc-text">8.2.2.按结果嵌套处理</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B9%9D%E3%80%81%E5%8A%A8%E6%80%81-SQL"><span class="toc-number">10.</span> <span class="toc-text">九、动态 SQL</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#9-1-IF"><span class="toc-number">10.1.</span> <span class="toc-text">9.1.IF</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-2-Where"><span class="toc-number">10.2.</span> <span class="toc-text">9.2.Where</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-3-Set"><span class="toc-number">10.3.</span> <span class="toc-text">9.3.Set</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-4-Sql-Include"><span class="toc-number">10.4.</span> <span class="toc-text">9.4.Sql-Include</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-5-Foreach"><span class="toc-number">10.5.</span> <span class="toc-text">9.5.Foreach</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%81%E3%80%81%E7%BC%93%E5%AD%98"><span class="toc-number">11.</span> <span class="toc-text">十、缓存</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#10-1-%E4%B8%80%E7%BA%A7%E7%BC%93%E5%AD%98"><span class="toc-number">11.1.</span> <span class="toc-text">10.1.一级缓存</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-2-%E4%BA%8C%E7%BA%A7%E7%BC%93%E5%AD%98"><span class="toc-number">11.2.</span> <span class="toc-text">10.2.二级缓存</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-3-%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BC%93%E5%AD%98"><span class="toc-number">11.3.</span> <span class="toc-text">10.3.自定义缓存</span></a></li></ol></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="footer_custom_text">却道天凉好个秋</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="Increase font size"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="Decrease font size"><i class="fas fa-minus"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      false && mermaid.init()
    })
  }
}</script></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>